package com.excel.service.excel.v1;



import com.corpgovernment.organization.entity.SyncEmployeeCallBackRequestEntity;
import com.corpgovernment.organization.entity.SyncOrganizationRequestEntity;
import com.corpgovernment.organization.enumm.ImportExcelRecordDetailLogErrorMessageEnum;
import com.corpgovernment.organization.enumm.OrgInfoSheetEnum;
import com.corpgovernment.organization.service.IOrganizationInfoService;
import com.corpgovernment.organization.util.ExcelUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.excel.model.ExcelImportConfig;
import com.excel.service.excel.*;
import com.excel.util.ExcelImportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 组织
 *
 * @author hc
 */
@Service
@Slf4j
public class OrgUpProcessorV1<PREINSERT> extends OrgAddProcessorV1<PREINSERT> implements ExcelImportProcessor<SyncEmployeeCallBackRequestEntity.FailInfo, SyncOrganizationRequestEntity.SyncOrganizationInfoEntity, BizImportTask, PREINSERT> {

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private IOrganizationInfoService organizationInfoService;

    @Autowired
    private ExcelImportUtil excelImportUtil;




    @Override
    public BizImportPreInsertResult<SyncEmployeeCallBackRequestEntity.FailInfo, SyncOrganizationRequestEntity.SyncOrganizationInfoEntity, PREINSERT> checkDbDataWithPreInsert(List<SyncOrganizationRequestEntity.SyncOrganizationInfoEntity> list) {


        BizImportPreInsertResultImpl<SyncEmployeeCallBackRequestEntity.FailInfo, SyncOrganizationRequestEntity.SyncOrganizationInfoEntity, PREINSERT> result = new BizImportPreInsertResultImpl();

        List<SyncEmployeeCallBackRequestEntity.FailInfo> errorList = new ArrayList<>();
        List<SyncOrganizationRequestEntity.SyncOrganizationInfoEntity> dataList = new ArrayList<>();


        List<String> needUpdateBusinessCodes = list.stream()
                .filter(o -> BooleanUtils.isNotTrue(o.getIsDelete()) && o.getBasicInfo() != null && StringUtils.isNotBlank(o.getBasicInfo().getBusinessCode()))
                .map(o -> o.getBasicInfo().getBusinessCode()).distinct().collect(Collectors.toList());

        List<String> needDelBusinessCodes = list.stream()
                .filter(o -> BooleanUtils.isTrue(o.getIsDelete()) && o.getBasicInfo() != null && StringUtils.isNotBlank(o.getBasicInfo().getBusinessCode()))
                .map(o -> o.getBasicInfo().getBusinessCode()).distinct().collect(Collectors.toList());

        Map<String, Boolean> updateBusinessCodeMap = excelImportUtil.isExistByFieldInDb("mb_org_info","business_code",needUpdateBusinessCodes,true);

        Map<String, Boolean> delBusinessCodeMap = excelImportUtil.isExistByFieldInDb("mb_org_info","business_code",needDelBusinessCodes,true);

        // 需要新增的信息 暂无
        List<PREINSERT> needInsertList = new ArrayList<>();


        // 新增 校验db是否存在的组织编码
        for (SyncOrganizationRequestEntity.SyncOrganizationInfoEntity item : list) {
            String itemBusinessCode = item.getBasicInfo().getBusinessCode();
            if(updateBusinessCodeMap.containsKey(itemBusinessCode) && !updateBusinessCodeMap.get(itemBusinessCode)){
                SyncEmployeeCallBackRequestEntity.FailInfo errorLog = new SyncEmployeeCallBackRequestEntity.FailInfo();
                errorLog.setMainLineNo(item.getLineNo());
                errorLog.setLineNo(item.getLineNo());
                errorLog.setSheetName(OrgInfoSheetEnum.INFO.getName());
                errorLog.setSheetIndex(OrgInfoSheetEnum.INFO.getIndex());
                errorLog.setFailReason(ImportExcelRecordDetailLogErrorMessageEnum.SyncHumanResources_SyncOrgInfo_Business_Code_Is_Empty.getErrorMessage());
                errorList.add(errorLog);
            }

            if(delBusinessCodeMap.containsKey(itemBusinessCode) && !delBusinessCodeMap.get(itemBusinessCode)){
                SyncEmployeeCallBackRequestEntity.FailInfo errorLog = new SyncEmployeeCallBackRequestEntity.FailInfo();
                errorLog.setMainLineNo(item.getLineNo());
                errorLog.setLineNo(item.getLineNo());
                errorLog.setSheetName(OrgInfoSheetEnum.INFO.getName());
                errorLog.setSheetIndex(OrgInfoSheetEnum.INFO.getIndex());
                errorLog.setFailReason(ImportExcelRecordDetailLogErrorMessageEnum.SyncHumanResources_SyncOrgInfo_Business_Code_Is_Empty.getErrorMessage());
                errorList.add(errorLog);
            }

            checkDeliveryData(errorList, item);

            checkCostCenterData(errorList, item);

            dataList.add(item);

        }

        result.setErrorList(errorList);
        result.setDataList(dataList);
        result.setPreInsertList(needInsertList);
        return result;
    }

    @Override
    public String getTag() {
        return "org_update_v1";
    }

    @Override
    public ExcelImportConfig getConfig() {
        ExcelImportConfig config = new ExcelImportConfig();
        config.setIsSharding(true);
        config.setIgnoreHeader(true);
        config.setRollBackType(1);
        return config;
    }

}
